<?php
class Feedback extends Dao
{
	private static $_instance = null;
	protected $_db = null, $_cache = null;
	public $_arrStatus = array('0' => 'Solved', '1'=>'New', '2'=>'Processing');
	
	protected function __construct()
	{
		parent::__construct();
	}
	
	public static function getInstance() 
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
	
	public function getList(array $args = array(), $intPage = 1, $intLimit = MIN_ROWS_PER_PAGE)
	{
		try{
			$defaults = array(
				'topic_id' => null
			);
			$args = $this->args_merge($defaults, $args);

			$intStart = ($intPage-1)*$intLimit;
				
			$strSql = " SELECT SQL_CALC_FOUND_ROWS DISTINCT *
						FROM tbl_user_feedback INNER JOIN tbl_user_feedback_topics USING (topic_id)
						WHERE topic_id = IFNULL(:topic, topic_id)
						LIMIT :intStart, :intLimit";
			
			$cmd = $this->_db->createCommand($strSql);
			$cmd->bindParam(":topic",$args['topic_id'],PDO::PARAM_INT);
			$cmd->bindParam(":intStart",$intStart,PDO::PARAM_INT);
			$cmd->bindParam(":intLimit",$intLimit,PDO::PARAM_INT);
			
			$arrResults = $cmd->queryAll();
			if(!empty($arrResults)){
				$arrResults[0]['total_rows'] = $this->_db->createCommand('SELECT FOUND_ROWS();')->queryScalar();
			}
			return $arrResults;
		} catch(Exception $ex){
			throw new Exception($ex->getMessage());
		}
	}
	
	public function insert(array $args)
	{
		try{
			$defaults = array(
				'topic_id' => null,
				'feedback_created_date' => new CDbExpression('NOW()'),
				'feedback_creator_id' => null,
				'feedback_creator_name' => null,
				'feedback_creator_email' => null,
				'feedback_creator_phone' => null,
				'feedback_creator_address' => null,
				'feedback_creator_ip' => Utils::getIp(),
				'feedback_title' => null,
				'feedback_content' => null,
				'feedback_note' => null,
				'feedback_status' => 1,
			);
			$args = $this->args_merge($defaults, $args);
			
			$cmd = $this->_db->createCommand();
			$cmd->insert('tbl_user_feedback',$args);
			return Yii::app()->db->getLastInsertId();
		} catch(Exception $ex){
			throw new Exception($ex->getMessage());
		}
	}
	
	public function update($id, array $args)
	{
		try{
			$defaults = array(
				'feedback_note' => null,
				'feedback_status' => 1,		
			);
			$args = $this->args_merge($defaults, $args);
			
			$cmd = $this->_db->createCommand();
			return $cmd->update('tbl_user_feedback',$args, 'feedback_id=:id', array(':id'=>$id));
		} catch(Exception $ex){
			throw new Exception($ex->getMessage());
		}
	}
	
	public function delete($id)
	{
		try{
			$cmd = $this->_db->createCommand();
			return $cmd->delete('tbl_user_feedback', 'feedback_id=:id', array(':id'=>$id));		
		} catch(Exception $ex){
			throw new Exception($ex->getMessage());
		}
	}
	
	public function getListTopics(array $args = array(), $intPage = 1, $intLimit = MIN_ROWS_PER_PAGE)
	{
		try{
			$defaults = array(

			);
			$args = $this->args_merge($defaults, $args);

			$intStart = ($intPage-1)*$intLimit;
				
			$strSql = " SELECT SQL_CALC_FOUND_ROWS DISTINCT *
						FROM tbl_user_feedback_topics
						ORDER BY topic_position ASC
						LIMIT :intStart, :intLimit";
			
			$cmd = $this->_db->createCommand($strSql);
			$cmd->bindParam(":intStart",$intStart,PDO::PARAM_INT);
			$cmd->bindParam(":intLimit",$intLimit,PDO::PARAM_INT);
			
			$arrResults = $cmd->queryAll();
			if(!empty($arrResults)){
				$arrResults[0]['total_rows'] = $this->_db->createCommand('SELECT FOUND_ROWS();')->queryScalar();
			}
			return $arrResults;
		} catch(Exception $ex){
			throw new Exception($ex->getMessage());
		}
	}
	
}